package cacy.leetcode.array;


/**
 * @author 凯齐
 * @date 2025-02-19 17:51
 * @description
 * 教练使用整数数组 actions 记录一系列核心肌群训练项目编号。
 * 为增强训练趣味性，需要将所有奇数编号训练项目调整至偶数编号训练项目之前。
 * 请将调整后的训练项目编号以 数组 形式返回。
 *
 * 示例 1：
 * 输入：actions = [1,2,3,4,5]
 * 输出：[1,3,5,2,4]
 * 解释：为正确答案之一
 *
 * 提示:
 *     0 <= actions.length <= 50000
 *     0 <= actions[i] <= 10000
 *
 */

class SolutionArray01 {
    public int[] trainingPlan(int[] actions) {
        if (actions.length == 0) {
            return new int[0];
        }
        int[] res = new int[actions.length];
        int j = 0;
        for (int i = 0; i < res.length; i++) {
            if (actions[i] % 2 == 1) {
                res[j] = actions[i];
                j++;
            }
        }
        for (int i = 0; i < res.length; i++) {
            if (actions[i] % 2 == 0) {
                res[j] = actions[i];
                j++;
            }
        }
        return res;
    }
}

// 不考虑奇偶数顺序
class SolutionArray01B {
    public int[] trainingPlan(int[] actions) {
        int i = 0, j = actions.length - 1, tmp;
        while (i < j) {
            while (i < j && (actions[i] & 1) == 1) {
                i++;
            }
            while (i < j && (actions[j] & 1) == 0) {
                j--;
            }
            tmp = actions[i];
            actions[i] = actions[j];
            actions[j] = tmp;
        }
        return actions;
    }
}

public class Array01 {
    public static void main(String[] args) {
        int[] test = {1,2,3,4,5};
        int[] res = new SolutionArray01().trainingPlan(test);
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + " ");
        }
    }
}
